﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using DreamBuilder.DictionaryAccess;
using EPlanet.Web.UI.WebControls;

namespace DreamBuilder.UI.UserControls
{
    public class ListTemplateBoundLink : ControlBase, ITemplate
    {
        CVoucherListItemInfo itemInfo = null;
        public event ClickedEventHandler CommandClick = null;

        public ListTemplateBoundLink() { }
        public string ImageUrl = null;
        public ListTemplateBoundLink(CVoucherListItemInfo voucherListItemInfo, string sImageUrl)
        {
            itemInfo = voucherListItemInfo;
            ImageUrl = sImageUrl;
        }

        #region ITemplate 成员
        // 初始化模板列
        void ITemplate.InstantiateIn(Control container)
        {
            VitCommand cmd = new VitCommand();
            cmd.CommandName = itemInfo.TableCode + "__" + itemInfo.FieldCode;
            cmd.IsForBoundField = true;
            if (!String.IsNullOrEmpty(itemInfo.NavigateURL))
            {
                cmd.ButtonType = VitCommand.EButtonType.All;
                cmd.ButtonImage = ImageUrl;
            }
            else
                cmd.ButtonType = VitCommand.EButtonType.Link;
            cmd.TextFieldName = itemInfo.TableCode + "__" + itemInfo.FieldCode;
            TableDicInfo tableDicInfo = DictionaryAccessFacade.GetTableDicInfo(itemInfo.TableCode);
            if (tableDicInfo != null)
            {
                 //绑定数据列
                cmd.ValueFieldName = itemInfo.TableCode + "__" + tableDicInfo.PrimaryField;
            }
            cmd.CommandClick += new EPlanet.Web.UI.WebControls.ClickedEventHandler(cmd_CommandClick);
            container.Controls.Add(cmd);
        }

        void cmd_CommandClick(object toSender, ClickEventArgs e)
        {
            if (this.CommandClick != null)
            {
                this.CommandClick(toSender, e);
            }
        }

        #endregion
    }
}
